package com.bclz.laziness

object StreamTest {

  def main(args: Array[String]): Unit = {

    println(Stream(1,2,3).take(2).toList)
    println(Stream(1,2,3,4,3,2,3,2,1,9,1,3).exists(_==9))
    println(Stream(1,2,3,4,3,2,3,2,1,9,1,3).map(_+10).filter(i=>(i & 1)==0).toList)

    //stream构造向前引用了ones变量，会编译错误
    //error:forward reference extends over definition of value ones
    //解决办法: 声明为lazy,或者直接在object中声明
    lazy val ones:Stream[Int]=Stream.cons(1,ones)
    println(ones.take(5).toList)


    println(Stream.fibs().takeWhileViaFoldRight(_==8).toList)
    println(Stream.fromViaUnfold(2).take(10).toList)
  }

  //直接在object中声明，让object继承App也是一样的效果
  val ones:Stream[Int]=Stream.cons(2,ones)
  println(ones.take(5).toList)

}
